package org.example.service;

import org.apache.poi.ss.usermodel.*;
import org.example.model.User;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ExcelImportService {
    public List<User> importUsers(File file) throws Exception{
        List<User> users = new ArrayList<>();
        FileInputStream fis = new FileInputStream(file);
        Workbook workbook = WorkbookFactory.create(fis);
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator= sheet.iterator();
        while (rowIterator.hasNext()){
            Row row = rowIterator.next();
            Cell usernameCell = row.getCell(0);
            Cell passwordCell = row.getCell(1);
            String username = usernameCell.getStringCellValue();
            String password = passwordCell.getStringCellValue();

            User user = new User();
            user.setUsername(username);
            user.setPassword(password);

            users.add(user);

        }
        workbook.close();
        fis.close();
        return users;
    }
}


























